package com.mtw.bbs.bbsUser.controller.user;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mtw.bbs.article.feign.LikeClient;
import com.mtw.bbs.bbsUser.pojo.entity.User;
import com.mtw.bbs.bbsUser.pojo.vo.UserCountVO;
import com.mtw.bbs.bbsUser.service.UserFollowService;
import com.mtw.bbs.bbsUser.service.UserService;
import com.mtw.bbs.common.core.security.UserDetails;
import com.mtw.bbs.common.core.security.context.SecurityContextHolder;
import com.mtw.bbs.common.core.vo.Result;
import com.mtw.bbs.userAdmin.pojo.vo.UserInfoVo;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

/**
 * 关注接口层
 */
@Slf4j
@RestController
@RequestMapping("user/follow")
@RequiredArgsConstructor
public class UserFollowController {

    private final LikeClient likeClient;
    private final UserService userService;
    private final UserFollowService userFollowService;



    /**
     * 获取当前用户的关注
     *
     * @param page 当前页
     * @param size 分页大小
     */
    @GetMapping(value = "/list")
    public Result getCurrentFollowList(@RequestParam(name = "page") int page,
                                       @RequestParam(name = "size") int size) {

        UserDetails user = SecurityContextHolder.getContext().getAuthentication();
        if (user == null) {
            return Result.success(new Page<>());
        }
        Page<UserInfoVo> voPage = userFollowService.getUserFollowPage(user.getId(), page, size);
        return Result.success(voPage);
    }


    /**
     * 获取用户的关注
     *
     * @param username   用户username
     * @param page 当前页
     * @param size 分页大小
     */
    @GetMapping(value = "/list/{username}")
    public Result getFollowList(@PathVariable("username") String username,
                                @RequestParam(name = "page") int page,
                                @RequestParam(name = "size") int size) {

        User user = userService.getUserInfoByUsername(username);
        if (user == null) {
            return Result.success(new Page<>());
        }
        Page<UserInfoVo> voPage = userFollowService.getUserFollowPage(user.getId(), page, size);
        return Result.success(voPage);
    }


    /**
     * 获取当前用户的粉丝
     *
     * @param page 当前页
     * @param size 分页大小
     */
    @GetMapping(value = "/fans")
    public Result getFansList(@RequestParam(name = "page") int page,
                              @RequestParam(name = "size") int size) {

        UserDetails user = SecurityContextHolder.getContext().getAuthentication();
        if (user == null) {
            return Result.success(new Page<>());
        }
        Page<UserInfoVo> voPage = userFollowService.getUserFansList(user.getId(), page, size);
        return Result.success(voPage);
    }


    /**
     * 获取用户的粉丝
     *
     * @param username   用户username
     * @param page 当前页
     * @param size 分页大小
     */
    @GetMapping(value = "/fans/{username}")
    public Result getFansList(@PathVariable("username") String username,
                              @RequestParam(name = "page") int page,
                              @RequestParam(name = "size") int size) {

        User user = userService.getUserInfoByUsername(username);
        if (user == null) {
            return Result.success(new Page<>());
        }
        Page<UserInfoVo> voPage = userFollowService.getUserFansList(user.getId(), page, size);
        return Result.success(voPage);
    }


    /**
     * 用户关注
     * @param username  被关注用户username
     */
    @PostMapping(value = "/{username}")
    public Result follow(@PathVariable("username") String username) {

        UserDetails currentUser = SecurityContextHolder.getContext().getAuthentication();
        User user = userService.getUserInfoByUsername(username);
        if (currentUser == null || user == null) {
            return Result.fail("请登录");
        }
        if (currentUser.getId().equals(user.getId())){
            return Result.success();
        }
        boolean result = userFollowService.followUser(currentUser.getId(), user.getId());
        return result?Result.success(1):Result.fail("关注失败");
    }



    /**
     * 用户取消关注
     * @param username  被关注用户username
     */
    @DeleteMapping(value = "/{username}")
    public Result cancelFollow(@PathVariable("username") String username) {

        UserDetails currentUser = SecurityContextHolder.getContext().getAuthentication();
        User user = userService.getUserInfoByUsername(username);
        if (currentUser == null || user == null) {
            return Result.fail("请登录");
        }
        if (currentUser.getId().equals(user.getId())){
            return Result.success();
        }
        boolean result = userFollowService.cancelFollowUser(currentUser.getId(), user.getId());
        return result?Result.success(1):Result.fail("取消关注失败");
    }


    /**
     * 获取当前用户的关注数与粉丝数
     *
     */
    @GetMapping(value = "/follow_and_fans_count")
    public Result getUserFollowAndFansCount() {

        UserCountVO vo = new UserCountVO();
        UserDetails user = SecurityContextHolder.getContext().getAuthentication();
        if (user == null) {
            return Result.success(vo);
        }

        Integer like = likeClient.getUserLikeCount(user.getId()).getData();
        Integer follow = userFollowService.countOfFollow(user.getId());
        Integer fans = userFollowService.countOfFans(user.getId());
        vo.setFans(fans);
        vo.setFollow(follow);
        vo.setLike(like);
        return Result.success(vo);
    }




}
